package com.clw.service.impl;


import com.clw.dataobject.User;
import com.clw.repository.UserRepository;
import com.clw.service.UserService;
import com.clw.utils.DateUtils;
import com.clw.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017-12-15.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository repository;

    @Override
    public boolean hasMore(String userName) {
        User user=repository.findByUserName(userName);
        if (user!=null){
            return true;
        }
        return false;
    }

    @Override
    public List<User> findAll() {
        return repository.findAll();
    }

    @Override
    public User save(User user) {
        return repository.save(user);
    }



    @Override
    public void del(User user) {
        repository.delete(user);
    }

    @Override
    public User add(User user) {
        user.setCreateTime(new Date());
        user.setStatus(0);
        return repository.save(user);
    }

    @Override
    public User findByUserName(String userName) {
        return repository.findByUserName(userName);
    }

    @Override
    public User findOne(Integer id) {
        return repository.findOne(id);
    }

    @Override
    public Page<User> findAll(String nickname, String startTime, String endTime, Pageable pageable) {
        return repository.findAll(getSpecification(nickname,startTime,endTime),pageable);
    }

    @Override
    public Long getSize(String nickname, String startTime, String endTime) {
        return repository.count(getSpecification(nickname,startTime,endTime));
    }

    private Specification<User> getSpecification(String nickname, String startTime, String endTime){
        return new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates=new ArrayList<>();
                if (StringUtils.isNotEmpty(nickname)){
                    predicates.add(criteriaBuilder.like(root.get("nickname"),"%"+nickname+"%"));
                }

                if (StringUtils.isNotEmpty(startTime)){
                    Date start= DateUtils.stringToDate(startTime);
                    predicates.add(criteriaBuilder.greaterThan(root.get("createTime"),start));
                }
                if (StringUtils.isNotEmpty(endTime)){
                    Date end=DateUtils.addDay(endTime,1);
                    predicates.add(criteriaBuilder.lessThan(root.get("createTime"),end));
                }
                return criteriaQuery.where(predicates.toArray
                        (new Predicate[predicates.size()])).getRestriction();
            }
        };
    }
}
